package jnBase.view
{
	import jnBase.Data;
	
	import laya.ui.Dialog;
	import laya.utils.Handler;
	import laya.utils.Tween;
	
	import ui.tools.LoadingUI;
	
	public class PageLoading extends LoadingUI
	{
		private static var _ins:PageLoading;
		public function PageLoading()
		{
			super();
			size(1920,1080);
			this.autoDestroyAtClosed=true;
			this.popupEffect=new Handler(this,function(dialog:Dialog):void{
				dialog.scale(1, 1);
				dialog.alpha=1;
				dialog._effectTween = Tween.from(dialog, {alpha:0}, 300, null, Handler.create(this, this.doOpen, [dialog]), 0, false, false);
			})
			this.closeEffect=new Handler(this,function(dialog:Dialog):void{
				dialog.scale(1, 1);
				dialog.alpha=1;
				dialog._effectTween = Tween.to(dialog, {alpha:0}, 300, null, Handler.create(this, this.doClose, [dialog]), 0, false, false);
			})
		}
		override public function onAwake():void
		{
			// TODO Auto Generated method stub
			super.onAwake();
			event("loadReady");
			tip.visible=false;
		}
		
		override public function onEnable():void
		{
			// TODO Auto Generated method stub
			super.onEnable();
			pro.value=0;
			bar.x=0;
			lab_Pro.text="正在加载……";
			loop();
		}
		private function loop():void{
			tick=Math.floor(Data.loadurlList.length*Math.random());
			if(Data.loadurlList[tick]){
				tip.text=Data.loadurlList[tick].des;
			}
			img.loadImage(Data.loadurlList[tick].url);
			Laya.timer.once(5000,this,again);
		}
		private function imgReady():void
		{
			// TODO Auto Generated method stub
			Laya.timer.once(5000,this,again);
		}
		private var tick:int=0;
		private function again():void
		{
			Tween.to(boxTip,{alpha:0,x:0},600,null);
			if(tick>Data.loadurlList.length-1){
				tick=0;
			}
			if(Data.loadurlList[tick]){
				tip.text=Data.loadurlList[tick].des;
			}
			img.loadImage(Data.loadurlList[tick].url);
			Laya.timer.once(5000,this,imgReady);
			tick++;
			boxTip.alpha=0;
			boxTip.x=1080;
			Tween.to(boxTip,{alpha:1,x:541},600);
		}
		public static function getInstance():PageLoading{
			 _ins||=new PageLoading();
			 _ins.show();
			 return _ins;
		}
		public function progress(n:Number):void
		{
//			trace("progress:"+n);
			if(lab_Pro) lab_Pro.text=Math.floor(n*100)+"%";
			if(pro) pro.value=n;
			if(bar) bar.x=pro.width*pro.value;
			if(n>=1){
				this.removeSelf();
				close();
				Laya.timer.clear(this,again);
			}
		}
		override public function onDestroy():void{
		}
		public function setTitle($str:String):void{
			title.text=$str;
		}
		
	}
}